﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using UncleBobWebService.Models;
using UncleBobWebService.ServiceRequester;


namespace UncleBobApplicantWebService.Controllers
{
    public class JobQuestionsController : Controller
    {
        private UncleBobApplicantWebService.ApplicantWebService.WebServiceClient mApplicantClient = new UncleBobApplicantWebService.ApplicantWebService.WebServiceClient();
        private JobModel job = new JobModel();
        //
        // GET: /JobQuestions/

        public ActionResult JobQuestionsEnter()
        {
            job = Session[SessionKeys.JOB] as JobModel;           
            return View();
        }

        public ActionResult FormAction(FormCollection collection)
        {
            job = Session[SessionKeys.JOB] as JobModel;

            if (collection["questionSubmit"] != null)
            {
                string question;
                ApplicationQuestions appQs = new ApplicationQuestions();

                question = collection.Get("question");

                if (ValueNotNull(collection, "answer1"))
                {
                    MultipleChoiceQuestion mQ = new MultipleChoiceQuestion();
                    mQ.Question = question;

                    if (ValueNotNull(collection, "answer1"))
                    {
                        Answer a = new Answer(collection.Get("answer1"),IsChecked(collection, "answerCorrect1"));
                        mQ.AddPossibleAnswer(a);
                    }

                    if (ValueNotNull(collection, "answer2"))
                    {
                        Answer a = new Answer(collection.Get("answer2"), IsChecked(collection, "answerCorrect2"));
                        mQ.AddPossibleAnswer(a);
                    }

                    if (ValueNotNull(collection, "answer3"))
                    {
                        Answer a = new Answer(collection.Get("answer3"), IsChecked(collection, "answerCorrect3"));
                        mQ.AddPossibleAnswer(a);
                    }

                    if (ValueNotNull(collection, "answer4"))
                    {
                        Answer a = new Answer(collection.Get("answer4"), IsChecked(collection, "answerCorrect4"));
                        mQ.AddPossibleAnswer(a);
                    }

                    if (ValueNotNull(collection, "answer5"))
                    {
                        Answer a = new Answer(collection.Get("answer5"), IsChecked(collection, "answerCorrect5"));
                        mQ.AddPossibleAnswer(a);
                    }

                    appQs.AddQuestion(mQ);

                }
                else
                {
                    ExtendedQuestion eX = new ExtendedQuestion(question);
                    appQs.AddQuestion(eX);
                }

                appQs.JobID = job.ID;
                Message msg = new Message(MessageTypes.SERVICE_CREATE_QUESTIONS);
                msg.Data = appQs;
                mApplicantClient.RequestService(msg);

                if (!msg.Result)
                {
                    //TempData(SessionKeys.ERROR) = "Unable to save questions to the database.";

                }
            }
          
            return RedirectToAction("QuestionsMenu", "QuestionsMenu");          
        }

        private bool IsChecked(FormCollection collection, string key)
        {
            if (collection.Get(key) != null)
                return true;
            return false;
        }

        private bool IsNotNull(FormCollection collection, string key)
        {
            if (collection.Get(key) != null)
                return true;
            return false;
        }

        private bool ValueNotNull(FormCollection collection, string key)
        {
            bool result = false;
            if (collection.Get(key) != null)
            {
                if (collection[key] != string.Empty)
                    result = true;
            }

            return result;
        }
    }
}
